package com.ibm.testbed.script.util;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Table;

public class TableTool {

	public static void setTableShowRows(TableViewer tableViewer,int num){
		if(num < 4){
			num = 4;
		}
		GridData layoutData = new GridData(768);
		layoutData.heightHint = num * 14;
		tableViewer.getTable().setLayoutData(layoutData);
	}
	
	public static TableViewer createTableViewerWithGroupEdit(Composite parent,String text,TableParam tableParam,final TableEditInterface tableEdit){
		return (TableViewer)TableTool.createTableViewerObjectsWithGroupEdit(parent, text, tableParam, tableEdit).get(0);
	}
	public static List<Object> createTableViewerObjectsWithGroupEdit(Composite parent,String text,TableParam tableParam,final TableEditInterface tableEdit){
		Group group = Layout.createTableGroup(parent, text);
		tableParam.setParent(group);
		
		final TableViewer tableViewer = TableTool.createTableViewer(tableParam);
		
		final Table table = tableViewer.getTable();
		((GridData)table.getLayoutData()).verticalSpan = 3;
		((GridLayout) group.getLayout()).numColumns++;
		
		Button button_add = createButton(group,"Add");
		
		button_add.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				tableEdit.handleAddButtonPressed();
			}
		});
		
		final Button button_edit = createButton(group,"Edit");
		button_edit.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int rowIndex = table.getSelectionIndex();
				tableEdit.handleEditButtonPressed(rowIndex);
			}
		});
		
		final Button button_delete = createButton(group,"Delete");
		button_delete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int rowIndex = table.getSelectionIndex();
				String[][] inputs = (String[][])tableViewer.getInput();
				String[][] params = new String[inputs.length-1][];
				for(int i=0;i<inputs.length;i++){
					if(rowIndex > i){
						params[i] = inputs[i];
					}else if(rowIndex < i){
						params[i-1] = inputs[i];
					}
				}
				tableViewer.setInput(params);
				tableEdit.handleDeleteRow(rowIndex);
			}
		});
		button_delete.setEnabled(false);
		button_edit.setEnabled(false);
		tableViewer.addSelectionChangedListener(new ISelectionChangedListener(){
			@Override
			public void selectionChanged(SelectionChangedEvent arg0) {
				int rowIndex = table.getSelectionIndex();
				if(rowIndex >= 0){
					button_delete.setEnabled(true);
					button_edit.setEnabled(true);
					tableEdit.handleSelectRow(rowIndex);
				}else{
					button_delete.setEnabled(false);
					button_edit.setEnabled(false);
					tableEdit.handleDeSelectRow();
				}
				
			}});
		List<Object> result = new ArrayList<Object>();
		result.add(tableViewer);
		result.add(button_add);
		result.add(button_edit);
		result.add(button_delete);
		return result;
	}
	public static void addTableViewerRow(String[] row,TableViewer tableViewer){
		String[][] inputs = (String[][])tableViewer.getInput();
		if(inputs == null){
			inputs = new String[0][];
		}
		String[][] params = new String[inputs.length+1][];
		for(int i=0;i<inputs.length;i++){
			params[i] = inputs[i];
		}
		params[inputs.length] = row;
		tableViewer.setInput(params);
	}
	public static void updateTableViewerRow(Integer rowIndex,String[] row,TableViewer tableViewer){
		String[][] inputs = (String[][])tableViewer.getInput();
		inputs[rowIndex] = row;
		tableViewer.setInput(inputs);
	}
	public static TableViewer createTableViewerWithGroup(Composite parent,String text,TableParam tableParam){
		tableParam.setParent(Layout.createTableGroup(parent, text));
		return TableTool.createTableViewer(tableParam);
	}
	public static TableViewer createTableViewer(TableParam tableParam){
		TableViewer tableViewer = new TableViewer(tableParam.getParent(), SWT.BORDER
				| SWT.FULL_SELECTION);

		final Table table = tableViewer.getTable();
		table.setHeaderVisible(tableParam.isHeaderVisible());
		table.setLinesVisible(tableParam.isLinesVisible());

		tableViewer.setContentProvider(ArrayContentProvider
				.getInstance());

		createComponentColumn(tableViewer,tableParam);
		TableTool.setTableShowRows(tableViewer, tableParam.getShowLines());
		return tableViewer;
	}
	
	public static void createComponentColumn(TableViewer tv,TableParam tableParam) {
		for (int index = 0; index < tableParam.getTitles().length; index++) {
			TableViewerColumn tvc = new TableViewerColumn(tv, SWT.NONE);
			tvc.getColumn().setWidth(tableParam.getWidth()/tableParam.getTitles().length);
			tvc.getColumn().setMoveable(true);
			tvc.getColumn().setResizable(true);
			tvc.getColumn().setText(tableParam.getTitles()[index]);
			tvc.getColumn().setAlignment(SWT.CENTER);
			
			tvc.setLabelProvider(new FileCellLabelProvider(tv, index));
		}
	}
	
	public static Button createButton(Composite parent, String label) {
 		Button button = new Button(parent, SWT.PUSH);
		button.setText(label);
		button.setFont(JFaceResources.getDialogFont());
		setButtonLayoutData(button);
 		return button;
 	}
	public static void setButtonLayoutData(Button button) {
		GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		int widthHint = 85;
		Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
		data.widthHint = Math.max(widthHint, minSize.x);
	 	button.setLayoutData(data);
	}
}
